//
//  ViewController.m
//  ReadBook
//
//  Created by Mac on 15-12-7.
//
//

#import "ViewController.h"
#import "ReadBookViewController.h"
@interface ViewController ()
{
    
    NSArray *imgs;
    NSArray *texts;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.title = @"书架";
    self.tableView.showsHorizontalScrollIndicator = NO;
    NSBundle *bundle = [NSBundle mainBundle];
     imgs = [bundle URLsForResourcesWithExtension:@"jpeg" subdirectory:nil];
    texts = [bundle URLsForResourcesWithExtension:@"txt" subdirectory:nil];
    //行高
    self.tableView.rowHeight = 139;
    //隐藏线条
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //警告
    UIAlertView *UIA = [[UIAlertView alloc]initWithTitle:@"1" message:@"2" delegate:nil cancelButtonTitle:@"3" otherButtonTitles:@"4", nil];
    [UIA show];

    
    
}

//backgroundview
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!imgs || imgs.count == 0) {
        return 0;
    }
    NSInteger count = imgs.count -1;
    return count/3+1;
    //return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    NSMutableArray * arr = [NSMutableArray array];
    //    for (NSURL *url in imgs) {
    //        [arr addObject:[UIImage imageNamed:url.lastPathComponent]];
    //    }
    NSInteger row = indexPath.row;
    UITableViewCell * cell = [UITableViewCell new];
    NSInteger index = row * 3;
    for (int i = 0; i < 3 && (index + i) < imgs.count; i++) {
        UIImage * image = [UIImage imageNamed:[imgs[index + i]lastPathComponent]];
        UIImageView * vi = [[UIImageView alloc] initWithImage:image];
        
        CGFloat edge = 15;
        CGFloat winth = (self.view.frame.size.width - edge * 4) / 3;
        vi.frame = CGRectMake(edge + (winth + edge) * i  , 8, winth, 139 - 8 - 16);
        [cell addSubview:vi];
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = vi.bounds;
        btn.tag = 100 + index +i;
        [btn addTarget:self action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
        [vi addSubview:btn];
        vi.userInteractionEnabled = YES;
        
    }
    cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BookShelfCell"]];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}

- (void)onClick:(UIButton *)sender
{
    NSInteger index = sender.tag -100;
    NSURL *imgURL = imgs[index];
    NSString *fileName = imgURL.lastPathComponent;
    fileName = fileName.stringByDeletingPathExtension;//[fileName stringByReplacingCharactersInRange:NSMakeRange(fileName.length - 5, 5) withString:@""];
    NSURL *tURL;
    for (NSURL *url in texts) {
        if ([url.absoluteString rangeOfString:fileName].location != NSNotFound) {
            tURL = url;
            break;
        }
    }
    
    NSError *err;
    NSString *content = [[NSString alloc] initWithContentsOfURL:tURL encoding:NSUTF8StringEncoding error:&err];
    if (err) {
        NSLog(@"%@",err.description);
    }
    ReadBookViewController *cos = [ReadBookViewController new];
    cos.text = content;
    cos.hidesBottomBarWhenPushed = NO;
    [self.navigationController pushViewController:cos animated:YES];

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ViewController * con = [ViewController new];
    [self.navigationController pushViewController:con animated:YES];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
